package com.xiaohei.activiti;

import com.xiaohei.ServiceAuthApplication;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: ProcessTestGateway
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/5/5 14:09
 * Version 1.0
 */
@ExtendWith(SpringExtension.class)  //集成Spring测试框架  扩展测试执行  提供上下文信息
@SpringBootTest(classes = ServiceAuthApplication.class)
public class ProcessTestGateway {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;


    //1.部署流程的定义
    @Test
    public void deployProcess(){
        //流程部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/qingjia002.bpmn20.xml")
                .addClasspathResource("process/wanzhengqingjia.png")
                .name("请假申请流程002")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    //2.启动流程实例
    @Test
    public void startProcess(){
        Map<String, Object> map = new HashMap<>();
        //设置请假的天数
        map.put("day","3");
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("qingjia002",map);

        System.out.println("流程定义id" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例id" + processInstance.getId());
    }

    //3. 查看待办
    @Test
    public void findTaskList(){
        String assign = "gousheng";
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(assign).list();
        for(Task task : list){
            System.out.println(task.getProcessInstanceId());
            System.out.println(task.getId());
            System.out.println(task.getAssignee());
            System.out.println(task.getName());
        }
    }

    //4. 任务的完成
    @Test
    public void completeTask(){
        Task task = taskService.createTaskQuery()
                .taskAssignee("gousheng")  //要查询的负责人
                .singleResult();//返回一条

        //完成任务,参数：任务id
        taskService.complete(task.getId());
    }


}
